/*
 * uithread.cpp
 *
 *  Created on: 2011-5-23
 *      Author: chenl
 */

#include "uithread.h"
#include "config.h"
#include "ps_error.h"
#include "uiapp.h"
#include "global.h"

#define DEFAULT_UITHREAD_LOGPREFIX	"./log/psui"

CUIThread::CUIThread() : logEngine(GET_UITHREADLOG_INSTANCE())
{

}

CUIThread::~CUIThread()
{

}

int32_t CUIThread::Initialize()
{
	logEngine.SetParam(GET_PARAMCONFIG_INSTANCE().GetLogLevel(), DEFAULT_UITHREAD_LOGPREFIX, &GET_LOGQUEUE_INSTANCE());
	int32_t ret = logEngine.Initialize();
	if (0 > ret)
	{
		return ret;
	}

	//初始化GUI引用層
	ret = CREATE_UIAPP_INSTANCE().Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing UIAPP failed! ret=0x%08x", ret);
		return ret;
	}

	return S_OK;
}

int32_t CUIThread::Uninitialize()
{
	//銷毀GUI應用層
	int32_t ret = GET_UIAPP_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing UIAPP failed! ret=0x%08x", ret);
		return ret;
	}

	ret = logEngine.Uninitialize();
	if (0 > ret)
	{
		return ret;
	}

	return S_OK;
}

void CUIThread::Execute()
{
	bool bIdle = true;
	bool bNeedReDraw = true;
	bool bHasCmd = true;

	CUIApp& uiApp = GET_UIAPP_INSTANCE();

	while((!IsTerminated()) || (!bIdle))
	{
		bIdle = true;

		//是否需要重畵
		bNeedReDraw = uiApp.IsUpdated() ? false : true;
		if (bNeedReDraw)
		{
			uiApp.DoUpdate();
			bIdle = false;
		}

		//處理命令
		bHasCmd = ProcessCommand();
		if (bHasCmd)
		{
			bIdle = false;
		}
		//處理定時器
		if (ProcessTimer())
		{
			bIdle = false;
		}
		if (bIdle)
		{
			DELAY(enmDefaultThreadExecutePeriod);
		}
	}
}

bool CUIThread::ProcessCommand()
{
	return false;
}

bool CUIThread::ProcessTimer()
{
	return false;
}
